﻿namespace Admin.NET.Core;

public static class LoggingSetup
{
    /// <summary>
    /// 日志注册
    /// </summary>
    /// <param name="services"></param>
    public static void AddLoggingSetup(this IServiceCollection services)
    {
        // 日志监听
        services.AddMonitorLogging(options =>
        {
            options.IgnorePropertyNames = new[] { "Byte" };
            options.IgnorePropertyTypes = new[] { typeof(byte[]) };
        });

        // 控制台日志
        var consoleLog = App.GetConfig<bool>("Logging:Monitor:ConsoleLog", true);
        services.AddConsoleFormatter(options =>
        {
            options.DateFormat = "yyyy-MM-dd HH:mm:ss(zzz) dddd";
            //options.WithTraceId = true; // 显示线程Id
            //options.WithStackFrame = true; // 显示程序集
            options.WriteFilter = (logMsg) =>
            {
                return consoleLog;
            };
        });

        // 日志写入文件
        if (App.GetConfig<bool>("Logging:File:Enabled", true))
        {
            //var loggingMonitorSettings = App.GetConfig<LoggingMonitorSettings>("Logging:Monitor", true);

            services.AddFileLogging(options =>
            {
                options.FileNameRule = fileName => string.Format(fileName, DateTime.Now, "Critical");
                options.WriteFilter = logMsg => logMsg.LogLevel == LogLevel.Critical;
            });

            services.AddFileLogging(options =>
            {
                options.WithTraceId = true; // 显示线程Id
                options.WithStackFrame = true; // 显示程序集
                options.FileNameRule = fileName => string.Format(fileName, DateTime.Now, "Error");
                options.WriteFilter = logMsg => logMsg.LogLevel == LogLevel.Error;
                options.HandleWriteError = (writeError) => // 写入失败时启用备用文件
                {
                    writeError.UseRollbackFileName(Path.GetFileNameWithoutExtension(writeError.CurrentFileName) + "-oops" + Path.GetExtension(writeError.CurrentFileName));
                };
            });

            services.AddFileLogging(options =>
            {
                options.FileNameRule = fileName => string.Format(fileName, DateTime.Now, "Warning");
                options.WriteFilter = logMsg => logMsg.LogLevel == LogLevel.Warning;
            });

            services.AddFileLogging(options =>
            {
                options.FileNameRule = fileName => string.Format(fileName, DateTime.Now, "Information");
                options.WriteFilter = logMsg => logMsg.LogLevel == LogLevel.Information;
            });

           

            //services.AddFileLogging(options =>
            //{
            //    options.WithTraceId = true; // 显示线程Id
            //    options.WithStackFrame = true; // 显示程序集
            //    options.FileNameRule = fileName => string.Format(fileName, DateTime.Now, logLevel.ToString()); // 每天创建一个文件
            //    options.WriteFilter = logMsg => logMsg.LogLevel >= logLevel; // 日志级别
            //    options.HandleWriteError = (writeError) => // 写入失败时启用备用文件
            //    {
            //        writeError.UseRollbackFileName(Path.GetFileNameWithoutExtension(writeError.CurrentFileName) + "-oops" + Path.GetExtension(writeError.CurrentFileName));
            //    };
            //    if (loggingMonitorSettings.JsonBehavior == JsonBehavior.OnlyJson)
            //    {
            //        options.MessageFormat = LoggerFormatter.Json;
            //        // options.MessageFormat = LoggerFormatter.JsonIndented;
            //        options.MessageFormat = (logMsg) =>
            //        {
            //            var jsonString = logMsg.Context.Get("loggingMonitor");
            //            return jsonString?.ToString();
            //        };
            //    }
            //});
        }

        // 日志写入ElasticSearch
        if (App.GetConfig<bool>("Logging:ElasticSearch:Enabled", true))
        {
            services.AddDatabaseLogging<ElasticSearchLoggingWriter>(options =>
            {
                options.WithTraceId = true; // 显示线程Id
                options.WithStackFrame = true; // 显示程序集
                options.IgnoreReferenceLoop = false; // 忽略循环检测
                options.MessageFormat = LoggerFormatter.Json;
                options.WriteFilter = (logMsg) =>
                {
                    return logMsg.LogName == CommonConst.SysLogCategoryName; // 只写LoggingMonitor日志
                };
            });
        }

        // 日志写入数据库
        if (App.GetConfig<bool>("Logging:Database:Enabled", true))
        {
            services.AddDatabaseLogging<DatabaseLoggingWriter>(options =>
            {
                options.WithTraceId = true; // 显示线程Id
                options.WithStackFrame = true; // 显示程序集
                options.IgnoreReferenceLoop = false; // 忽略循环检测
                options.WriteFilter = (logMsg) =>
                {
                    return logMsg.LogName == CommonConst.SysLogCategoryName; // 只写LoggingMonitor日志
                };
            });
        }
    }
}